const tools = require('../tools/tool');
const tableName = 'wx_zaina_gzh';
const cloumname = 'id,union_id,update_time,status,open_id,create_time,lotteries,lottery_amount';

//判断用户是否关注公众号
exports.isFocusWxPub = async (pool, unionId, openId) => {
    const sql = `select status from ${tableName} where union_id =?  and  open_id =  ?`;
    const result = await pool.query(sql, [unionId, openId]);
    if (result.length) {
        return tools.toTuoFeng(result[0]);
    }
    return null;
}

//添加转发记录
exports.addSharTextRecord = async (pool, options) => {
    const sqls = `insert into wx_forwarding_record set ?`;
    const result = await pool.query(sqls, options);
    return result.affectedRows > 0;
}

//转发文章，记录是否发过红包
exports.addRedEnvlopRecord = async (pool, options) => {
    const sqls = `insert into h5_red_record set ?`;

    const result = await pool.query(sqls, options);
    return result.affectedRows > 0;
}

//查询当天是否记录过红包情况，
exports.hasDateRedEnvlopRecord = async (pool, unionId, openId, dataStr, activeType) => {
    console.log('hasDateRedEnvlopRecord', unionId, openId, dataStr, activeType);
    const sqls = ` select *  from h5_red_record where open_id = ? and union_id = ? and date_str = ? and active_type = ? `;
    const result = await pool.query(sqls, [openId, unionId, dataStr, activeType]);
    if (result.length) {
        return tools.toTuoFeng(result[0]);
    }
    return null;
}


//批量给没有发红包的用户使用
exports.queryDateStrRedEnvByStatus = async (pool, status, dataStr) => {
    const sqls = `select *  from h5_red_record where status = ? and date_str = ? and active_type = 'shareText' limit 0,28`;
    const result = await pool.query(sqls, [status, dataStr]);
    if (result.length) {
        return tools.toTuoFeng(result);
    }
    return [];
}

//批量给没有发红包的用户使用
exports.batchUpdateRedEnvopStatus = async (pool, status = 1, openIds, dataStr, activeType) => {
    const sql = `update h5_red_record set status = ? where  open_id in (?)  and date_str = ? and status = 0 and active_type = ? `;
    const result = await pool.query(sql, [status, openIds, dataStr, activeType]);
    return result;
}